package com.dotflat.physicalevaluate.service.impl.chart;


import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;


@Service
public class RemoteBarChartServiceImpl extends AbstractRemoteHighChartService {

    private static final String JSON_TEMPLATE = "{\"infile\":{" +
            "\"credits\": {" +
            "\"enabled\": false" +
            "}," +
            "\"chart\": {" +
            "\"type\": \"column\"" +
            "}," +
            "\"title\": {" +
            "\"text\": \"#title\"" +
            "}," +
            "\"xAxis\": {" +
            "\"categories\": " +
            "[" +
            "#categories" +
            "]," +
            "\"crosshair\": true," +
            "\"labels\": {\"rotation\": #rotation}"+
            "}," +
            "\"yAxis\": {" +
            "\"min\": #min," +
            "\"max\": #max," +
            "\"title\": {" +
            "\"text\": \"\"" +
            "}," +
            "\"labels\": {" +
            //"  \"formatter\": function () {  return  this.value + '%';}" +
            "}" +
            "}," +
            "\"plotOptions\": {" +
            "\"column\": {" +
            "\"borderWidth\": 0" +
            "}," +
            "\"series\": {" +
            "\"borderWidth\": 0," +
            "\"dataLabels\": {" +
            "\"enabled\": false," +
            "\"align\": \"center\"," +
            "\"format\": \"{point.y}\"," +
            "\"y\": -5" +
            "}" +
            "}" +
            "}," +
            "\"series\": [#series]" +
            "}}";


    public byte[] getRemoteEvaluateChart(Map dataMap) {
        String title = (String)dataMap.get("title");
         String categories = (String)dataMap.get("categories");
         String series = (String)dataMap.get("series");
         String min = (String)dataMap.get("min");
         String max = (String)dataMap.get("max");
         String rotation = (String)dataMap.get("rotation");
//

         String jsonData = JSON_TEMPLATE.replace("#title",title);
         jsonData = jsonData.replace("#categories",categories);
         jsonData = jsonData.replace("#series",series);

         if(Objects.isNull(min)){
             min = "0";
         }
        if(Objects.isNull(max)){
            max = "100";
        }
        if(Objects.isNull(rotation)){
            rotation = "-45";
        }
        jsonData = jsonData.replace("#min",min);
        jsonData = jsonData.replace("#max",max);
        jsonData = jsonData.replace("#rotation",rotation);


        System.out.println("*********************begin******************");
        System.out.println(jsonData);
        System.out.println("**********************end*******************");
        //0,0,0,0,0,0,0
        return binaryHTTPCall(chartServerURL, jsonData, "创建测评结果图表时异常");
    }
}
